﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Workflow.Interface;
using MyLib;
using Workflow.Entity;
using Workflow.Logic;
using Portal;

namespace Workflow
{
    public partial class OpenForm : System.Web.UI.Page
    {
        ITask _task = ServiceContainer.Instance().GetService<ITask>();
        protected void Page_Load(object sender, EventArgs e)
        {
            string taskID = Request.QueryString["TaskID"];
            string type = Request.QueryString["Type"];
            string processName = Request.QueryString["ProcessName"];
            string incident = Request.QueryString["Incident"];
            string stepName = Request.QueryString["StepName"];
            string userName = "";

            string url = "";
            if (type != null && type.ToUpper().Trim() == "DRAFT") //从草稿打开
            {
                userName = Portal.Logic.SSOLogic.GetLoginName();
                if (processName != null && processName.Trim() != "")
                {
                    string strSql = "select c.initiateid from initiate c where c.processname = '" + processName + "'";
                    taskID = ConvertUtil.ToString(DataAccess.Instance("UltDB").ExecuteScalar(strSql)).Trim();
                }
                string page = StepSettingsLogic.GetDraftPage(processName);
                url = "http://" + HttpContext.Current.Request.Url.Host + ":"
                    + HttpContext.Current.Request.Url.Port + "/" + page + "?ProcessName=" + processName + "&StepName=" + stepName + "&Incident="
                   + incident + "&TaskID=" + taskID + "&UserName=" + HttpContext.Current.Server.UrlEncode(userName) + "&Type=" + type;
            }
            else if (type != null && type.ToUpper().Trim() == "PR2PO")
            {
                userName = Portal.Logic.SSOLogic.GetLoginName();
                if (processName != null && processName.Trim() != "")
                {
                    string strSql = "select c.initiateid from initiate c where c.processname = '" + processName + "'";
                    taskID = ConvertUtil.ToString(DataAccess.Instance("UltDB").ExecuteScalar(strSql)).Trim();
                }
                string page = StepSettingsLogic.GetDraftPage(processName);
                url = "http://" + HttpContext.Current.Request.Url.Host + ":"
                    + HttpContext.Current.Request.Url.Port + "/" + page + "?ProcessName=" + processName + "&StepName=" + stepName + "&Incident="
                   + incident + "&TaskID=" + taskID + "&UserName=" + HttpContext.Current.Server.UrlEncode(userName) + "&Type=" + type + "&Prno=" + Request.QueryString["prno"].ToString();
            }
            else if (type != null && type.ToUpper().Trim() == "EMAIL") //从邮件打开
            //例如：http://10.0.0.1/Modules/Workflow/OpenForm.aspx?Type=Email&ProcessName=%e5%8f%91%e7%a5%a8%e7%94%b3%e8%af%b7&Incident=14
            {
                userName = Portal.Logic.SSOLogic.GetLoginName();
                taskID = _task.GetTaskId(processName, ConvertUtil.ToInt32(incident), userName.Replace("\\", "/"));
                if (taskID == null)
                {
                    throw new Exception("Current user have no rights to view it!!");
                }
                url = _task.GetTaskUrl(taskID, type, userName.Replace("\\", "/"));
            }
            else
            {
                userName = Portal.Logic.SSOLogic.GetLoginName();
                if (string.IsNullOrEmpty(taskID)) //从报表打开
                {
                    url = _task.GetTaskUrl(_task.GetTaskId(processName, ConvertUtil.ToInt32(incident)), type, userName.Replace("\\", "/"));
                }
                else //从任务列表打开
                {
                    url = _task.GetTaskUrl(taskID, type, userName.Replace("\\", "/"));
                }
            }
            Response.Redirect(url);

        }
    }
}